<template>
  <a-modal
      width='1080px'
      title='交付物上传'
      cancelText="取消"
      okText="确定"
      :visible="visible"
      @ok="handleOk"
      :confirmLoading="confirmLoading"
      @cancel="handleCancel">
      <a-tabs defaultActiveKey="1" @change="callback">
        <a-tab-pane tab="从文件上传" key="1">
          <uploadFromFolder @selectChange='selectChangeHandler'></uploadFromFolder>
        </a-tab-pane>
        <a-tab-pane tab="本地上传" key="2" forceRender>
          <commonUpload
          style="width: 618px; margin: auto"
          :oid='stringOid'
          :multiple='true'
          :fileAddressDefault='fileAddressDefault'
          @selectFileType='selectFileType'
          @selectFileAddress='selectFileAddress'
          @handleUpload='handleUpload'
          @handleRemove='handleRemove'/>
        </a-tab-pane>
      </a-tabs>
  </a-modal>
</template>
<script>
let oId = JSON.parse(sessionStorage.getItem('oid'))
import uploadFromFolder from '@/components/uploadDelivery/src/components/uploadFromFolder'
import commonUpload from '@/components/common/commonUpload'
import copyACtion from '@/views/singleProject/fileSingle/el-common-js/copy'
export default {
  mixins: [copyACtion],
  data() {
    return {
      stringOid: '',
      visible: true,
      confirmLoading: false,
      types: '',
      trees: [],
      fileArr: [],
      fileAddressDefault: [],
      tabsIndex: '1',
      selectListArr: [],
    }
  },
  props: {
    startSingleID: {
      type: String,
      default: ''
    },
    // 进入不同方法
    startType: {
      type: String,
      // default: 'addDocProcessReviewObj'
      default: 'addDocProcessReviewObj'
    }
  },
  created () {
    this.stringOid = oId + ''
  },
  components: {
    uploadFromFolder,
    commonUpload
  },
  methods: {
    callback (val) {
      this.tabsIndex = val
    },
    /**
     * commonUpload 组件回调
     * 功能： props 选择选择文件类型
     */
    selectFileType(props) {
      this.types = props
    },
    /**
     * commonUpload 组件回调
     * 功能： props 选择文件存放位置
     */
    selectFileAddress (props) {
      this.fileAddressDefault = props
      this.trees = props
    },
    /**
     * commonUpload 组件回调
     * 功能： props 选择交付物文件
     */
    handleUpload (info) {
      this.fileArr.push(info)
    },
    /**
     * commonUpload 组件回调
     * 功能：
     *      file 需要删除的文件
     *      fileList 删除之后的文件数组
     */
    handleRemove (file, fileList) {
      this.fileArr = fileList
    },
    selectChangeHandler (val) {
      console.log(val)
      this.selectListArr = val
    },
    handleOk () {
      if (this.tabsIndex === '1') {
        this.selsetUpFile()
      } else if (this.tabsIndex === '2') {
        this.localUpFile()
      }
      // this.$emit('closeUpFile')
    },
    /**
     * 本地上传
     */
    localUpFile () {
      let _this = this
      let rules = {
        '请选择交付物类型....' : !this.types,
        '请选择交付物位置....' : this.trees.length < 1,
        '请选择交付物文件....' : this.fileArr.length < 1
      }
      // console.log(this.startType)
      this.upFile({
        types: this.types,
        trees: this.trees,
        fileArr: this.fileArr,
        defaultArr: this.fileAddressDefault,
        rules: rules,
        effectKey: this.startType,
        otherObj: {startSingleID: this.startSingleID},
        callback: (res) => {
          // debugger
          console.log(res)
          _this.confirmLoading = true
          if (res.code === 200) {
            // _this.$emit('closeUpFile')
            _this.confirmLoading = false
            if (_this.startType === 'callbackData') {
              console.log(res)
              console.log(_this.startType)
              // debugger
              _this.$emit('callbackData', res.data)
              this.$emit('closeUpFile')
              // return
            }else{
              _this.$emit('initTableData')
            }
          }
        }
      })
    },
    /**
     * 列表选择上传
     */
    selsetUpFile () {
      let _this = this
      let rules = {
        '请选择交付物文件....' : this.selectListArr.length < 1
      }
      if (this.startType === 'callbackData') {
        this.$store.commit('SETDOCTIMEDATA', this.selectListArr)
        this.$emit('callbackData', this.selectListArr)
        this.$emit('closeUpFile')
        return
      }
      this.addDocProcessReviewObj({
        rules: rules,
        data: this.selectListArr,
        oid: this.startSingleID,
        callback: (res) => {
          this.confirmLoading = true
          if (res.code === 200) {
            // _this.$emit('closeUpFile')
            // _this.visible = false
            _this.confirmLoading = false
            _this.$emit('initTableData')
          }
        }
      })
    },
    /**
     * 取消按钮
     */
    handleCancel () {
      // this.visible = false
      this.$emit('closeUpFile')
    },
    /**
     * 开启弹窗
     * key 上级需要开启那个弹窗的key
     */
    openModal (start) {
      this.visible = start
    }
  }
}
</script>
<style lang="less" scoped>
.common-upload {
  /deep/.ant-upload {
    width: 518px;
    margin-right: 0;
  }
  /deep/.ant-upload-list {
    margin-right: 35px;
  }
}
</style>